package com.gzs.learn.payment.api;

import com.gzs.learn.common.AjaxResult;
import com.gzs.learn.common.util.JsonUtil;
import com.gzs.learn.payment.inf.exception.PaymentException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 *
 * @author guanzhisong
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 支付异常
     */
    @ExceptionHandler(PaymentException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseEntity<String> paymentException(PaymentException e) {
        log.error("payment exception", e);
        AjaxResult ajaxResult = AjaxResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
        return new ResponseEntity<>(JsonUtil.toJSONString(ajaxResult), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 基础异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseEntity<String> baseException(Exception e) {
        log.error("internal server exception", e);
        AjaxResult ajaxResult = AjaxResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
        return new ResponseEntity<>(JsonUtil.toJSONString(ajaxResult), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
